﻿Public Class frmDonVi
    Private ctlDonVi As New CDonViController
    Private currDonVi As tblDonVi
    Private sSearch As String = ""

    Private Sub ucCountries_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'hiển thị danh sách các nước
        LoadListBox()
        'hien thi nut them, xoa, sua, thoat
        HienThiNut_Thuong()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    'Private Sub frmDonVi_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    '    If (e.KeyCode = Keys.N AndAlso e.Modifiers = Keys.Control) Then
    '        'thêm mới
    '        btnAdd_Click(Nothing, Nothing)
    '    End If
    '    If (e.KeyCode = Keys.E AndAlso e.Modifiers = Keys.Control) Then
    '        'Sửa
    '        btnEdit_Click(Nothing, Nothing)
    '    End If
    '    If (e.KeyCode = Keys.Delete) Then
    '        'xoá
    '        btnDel_Click(Nothing, Nothing)
    '    End If
    'End Sub
#Region "Button Event"
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If (MessageBox.Show("Bạn có chắc muốn xoá " + currDonVi.Ten + " ?", "Xác nhận", MessageBoxButtons.YesNo) = vbYes) Then
            ctlDonVi.DeleteRecord(currDonVi.ID_DonVi)
            'load lại grid
            LoadListBox()
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'kiểm tra có trùng tên
        If (txtTen.Text = "") Then
            MessageBox.Show("Thông tin tên chưa nhập.")
            txtTen.Focus()
            Exit Sub
        End If
        Dim trungTen As Boolean = False
        'kiểm tra xem tên có bị trùng hay không
        If (ctlDonVi.CheckIfExists(currDonVi.ID_DonVi, txtTen.Text)) Then
            MessageBox.Show("Đã tồn tại.")
            txtTen.Focus()
            Return
        End If

        'luu du lieu 
        With currDonVi
            .Ten = txtTen.Text
        End With
        Dim idCurr As Integer = currDonVi.ID_DonVi
        If currDonVi.ID_DonVi > 0 Then
            ctlDonVi.UpdateRecordWithoutAttach(currDonVi)
        Else
            ctlDonVi.InsertRecord(currDonVi)
            idCurr = currDonVi.ID_DonVi
        End If
        LoadListBox()
        'Dim indexRow As Integer = -1
        ''lấy ra vị trí của port vừa thêm hoặc chỉnh sửa vào
        'indexRow = FindIndexOfPortInGrid(idCurr)
        ''Set current row
        ''gridDonVi.CurrentCell = gridDonVi.Rows(indexRow).Cells(1)
        ''gridDonVi.Rows(indexRow).Selected = True
        ''tro ve trạng thái thường
        HienThiNut_Thuong()
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        txtTen.Enabled = True
        HienThiNut_Sua()
        RefreshForm()
        'Init obj
        currDonVi = New tblDonVi
        txtTen.Focus()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'gridDonVi.ReadOnly = False
        'gridDonVi.AllowUserToAddRows = False
        HienThiNut_Sua()
        txtTen.Focus()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Dim _id As Integer = gridDonVi.CurrentRow.Cells("ID_DonVi").Value
        'currDonVi = ctlDonVi.SelectRecordByID(_id)
        FillDonViForm()
        'hien thi lai trang thai binh thuong
        HienThiNut_Thuong()
    End Sub
#End Region

#Region "Utilities"
    'Private Function FindIndexOfPortInGrid(ByVal id As Integer) As Integer
    '    Dim index As Integer = 0
    '    For Each row As DataGridViewRow In gridDonVi.Rows
    '        If (row.Cells("ID_DonVi").Value = id) Then
    '            index = row.Index
    '            Exit For
    '        End If
    '    Next
    '    Return index
    'End Function
    Private Sub FillDonViForm()
        txtTen.Text = currDonVi.Ten
    End Sub
    Public Sub RefreshForm()
        'txtMuc.Text = ""
        'txtTieuMuc.Text = ""
        'txtTietMuc.Text = ""
        txtTen.Text = ""
    End Sub
    Private Sub EnableControls(ByVal val As Boolean)
        lsDonVi.Enabled = Not val
        txtTen.Enabled = val
    End Sub
    Private Sub HienThiNut_Thuong()
        'gridDonVi.AllowUserToAddRows = False
        'gridDonVi.AllowUserToDeleteRows = False
        'gridDonVi.ReadOnly = True
        EnableControls(False)
        btnAdd.Enabled = True
        btnDel.Enabled = True
        btnEdit.Enabled = True
        btnUpdate.Enabled = False
        btnCancel.Enabled = False
    End Sub
    Private Sub HienThiNut_Sua()
        EnableControls(True)
        btnAdd.Enabled = False
        btnDel.Enabled = False
        btnEdit.Enabled = False
        btnUpdate.Enabled = True
        btnCancel.Enabled = True
    End Sub
#End Region

#Region "ListBox"
    Public Sub LoadListBox()
        CViewer.BindDonViToListBox(lsDonVi)
    End Sub
    Private Sub lvChungTu_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsDonVi.SelectedIndexChanged
        If (lsDonVi.SelectedItems.Count > 0) Then
            Dim idDonVi As Integer = lsDonVi.SelectedValue
            'lấy thông tin danh muc
            currDonVi = ctlDonVi.SelectRecordByID(idDonVi)
            FillDonViForm()
        Else
            currDonVi = Nothing
        End If
    End Sub
#End Region
End Class